class Solution {
    public int maxTurbulenceSize(int[] arr) {
        int n = arr.length;
        int[] f = new int[n];
        int[] g = new int[n];
        for(int i = 0; i < n; i++) {
            f[i] = 1;
            g[i] = 1;
        }
        int ret = 1;
        for(int i = 1; i < n; i++) {
            if(arr[i-1] > arr[i]) {
                g[i] = f[i-1] + 1;
            }
            if(arr[i-1] < arr[i]) {
                f[i] = g[i-1] + 1;
            }
            ret = Math.max(ret, Math.max(f[i], g[i]));
        }
        return ret;
    }
}